import { EventMgr } from "../../../scripts/GlobalMessage";
import Singleton from "../../../scripts/Singleton";
import { URLData } from "../../../scripts/URLData";
import { netMgr } from "../../../scripts/net/NetMgr";
import { KugoEvent, KugoPiecesType } from "./KugoConst";
import { KugoModel } from "./KugoModel";
import { ChessPoint, DropChessReply, DropChessRequest, GameResultReply, GameStartReply, GameState, JoinRoomReply,
     JoinRoomRequest, KillRequest, LoginReply, LoginRequest, 
     ReadyState, UserReadyReply, UserReadyRequest, WSCmd } from "./proto/proto";

export class KugoControl extends Singleton<KugoControl>{

    private _model:KugoModel = KugoModel.getInstance()
    
    registerEvent(){
        netMgr.addResponeHandler(WSCmd.Login, this.s2cLogin.bind(this))
        netMgr.addResponeHandler(WSCmd.JoinRoom, this.s2cJoinRoom.bind(this))
        netMgr.addResponeHandler(WSCmd.UserReady, this.s2cUserReady.bind(this))
        netMgr.addResponeHandler(WSCmd.GameStart, this.s2cGameStart.bind(this))
        netMgr.addResponeHandler(WSCmd.DropChess, this.s2cDropChess.bind(this))
        netMgr.addResponeHandler(WSCmd.GameResult, this.s2cGameResult.bind(this))
    }
    
    c2sLogin(){
        let userId = URLData.userId
        let roomId = URLData.roomId
        if (!userId || userId == "") {
            console.log("请输入用户名")
            return
        }
        
        let sendData:LoginRequest = {
            WXCode: userId,
            NickName:"巅峰玩家" + userId,
            HeadUrl:"",
            RoomId: roomId
        }

        netMgr.send(WSCmd.Login,sendData)
    }

    /**发送落子 */
    c2sDropPieces(point:ChessPoint[]){
        let userInfo  = this._model.UserMgr.getUserById(URLData.userId)
        if (this._model.ChessBase.CurSeat != userInfo.SeatId) {
            console.error("不该你出手")
            return
        }
        if (this._model.ChessBase.GameState > GameState.DropPieces && point.length == 1) {
            console.error("不是落子阶段，数据错误", point)
            return
        }
        
        let sendData:DropChessRequest = {
            Point: point,
        }
        netMgr.send(WSCmd.DropChess, sendData)
    }

    /**选择吃子 */
    c2sSelectKill(point:ChessPoint[],kill:ChessPoint[]){
        let sendData:KillRequest = {
            Point: point,
            KillPoint:kill
        }
        netMgr.send(WSCmd.DropChessSelectKill, sendData)
    }

    /**准备 */
    c2sUserReady(){
        let sendData: UserReadyRequest ={
            State: ReadyState.READY
        }

        netMgr.send(WSCmd.UserReady, sendData)
    }


    /**返回 */
    s2cLogin(cmd:string, data:LoginReply){
        console.log("登录返回", data)
        let join:JoinRoomRequest = {}
        join.RoomId = data.RoomId

        netMgr.send(WSCmd.JoinRoom, join)
    }

    s2cUserReady(cmd:string, data:UserReadyReply){
        console.log("用户准备", data)
        this._model.UserMgr.userReady(data.SeatId, data.State)
        EventMgr.send(KugoEvent.RefreshUser)
    }

    s2cJoinRoom(cmd:string, data:JoinRoomReply){
        console.log("用户进入房间", data)
        data.UserList.forEach(user=>{
            this._model.UserMgr.addUserInfo(user)
        })
        EventMgr.send(KugoEvent.RefreshUser)
    }
    
    s2cGameStart(cmd:string, data:GameStartReply){
        console.log("游戏数据", data)
        this._model.ChessBase.setGameState(data.State)
        this._model.ChessBase.reconectInit(data.ChessData as any)
        this._model.ChessBase.setCurSeat(data.CurOptUser)
        EventMgr.send(KugoEvent.RefreshChess)
        data.Users.forEach(item=>{
            this._model.UserMgr.addUserInfo(item)
        })
    }

    s2cDropChess(cmd:string, data:DropChessReply){
        console.log("落子结果", data)

        this._model.ChessBase.setGameState(data.State)
        this._model.ChessBase.reconectInit(data.ChessData as any)
        EventMgr.send(KugoEvent.RefreshChess)

        this._model.ChessBase.setCurSeat(data.NextOptUser)
        this._model.UserMgr.refreshLeftTime(data.NextOptUser, data.Timestamp)
        EventMgr.send(KugoEvent.RefreshUser)

        EventMgr.send(KugoEvent.DropChessPieces,data)
    }

    s2cGameResult(cmd:string, data:GameResultReply){
        console.log("游戏结果", data)

    }
}

export let kugoControl = KugoControl.getInstance()